package com.zl.learning.db.mongo;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.*;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.client.model.Filters;
import com.zl.learning.db.mongo.entity.User;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;

public class TestMongo2 {

    private MongoClient getMongoClient() {
        CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build()));
        ConnectionString connString = new ConnectionString("mongodb://root:*Jsnjcc*@172.31.4.76/admin");
        MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(connString).codecRegistry(pojoCodecRegistry).retryWrites(true).build();
        return MongoClients.create(settings);
    }

    @Test
    public void test1() {
        try (MongoClient mongoClient = getMongoClient();) {
            MongoDatabase db = mongoClient.getDatabase("test");
            MongoCollection<User> collection = db.getCollection("user", User.class);
            User user = new User();
            user.setId(0);
            user.setUsername("张三");
            user.setPassword("123456");
            collection.insertOne(user);
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    @Test
    public void test2() {
        try (MongoClient mongoClient = getMongoClient();) {
            MongoDatabase db = mongoClient.getDatabase("test");
            MongoCollection<User> collection = db.getCollection("user", User.class);
            FindIterable<User> userFindIterable = collection.find(Filters.eq("username", "张三"));
            MongoCursor<User> cursor = userFindIterable.iterator();
            if (cursor.hasNext()) {
                User user = cursor.next();
                System.out.println(user);
            }
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    @Test
    public void test3() {
        try (MongoClient mongoClient = getMongoClient();) {
            MongoDatabase myDatabase = mongoClient.getDatabase("mydb");
            GridFSBucket gridFSBucket = GridFSBuckets.create(myDatabase, "images");
            Path file = Paths.get("D:\\图片\\pic", "1.jpeg");
            try (InputStream in = Files.newInputStream(file)) {
                gridFSBucket.uploadFromStream(file.getFileName().toString(), in);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private URLConnection getUrlConnection(String dataUrl) throws IOException {
        URL url = new URL(dataUrl);
        URLConnection uc = url.openConnection();
        int timeOut = 10000;
        uc.setConnectTimeout(timeOut);
        uc.connect();
        return uc;
    }

    @Test
    public void test4() {
        try (MongoClient mongoClient = getMongoClient();) {
            MongoDatabase myDatabase = mongoClient.getDatabase("mydb");
            GridFSBucket gridFSBucket = GridFSBuckets.create(myDatabase, "images");
            try (InputStream in = getUrlConnection("https://p0.ssl.img.360kuai.com/t01ce889d47c5ddd5a9.webp").getInputStream()) {
                gridFSBucket.uploadFromStream("1.jpg", in);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
